#pragma once
#ifndef _MQTT_PUB__H
#define _MQTT_PUB__H
#include <Theron/Theron.h>
#include "singleton.h"
#include "CMQTTClient.h"
#include <string>
#include <map>
#include <vector>
#include <mutex>
#include "DataCenter.h"
#include "Timer.h"

typedef vector<void*> ArgumentV;

enum {
	MQTTPUB_THREAD_ADDRESS = 0,
	MQTTPUB_PARAM_ONE,
};
typedef map<int, void*> ArgumentM;   //in this case,we save two params(a thread_addr and an other param) in ArgumentM,the two key is 'THREAD_ADDRESS'
									//and 'PARAM_ONE'
									//usually,if we has a thread_addr and n params(totally n+1),we should has a enum that is bellow
									// enum{
									//    THREAD_ADDRESS = 0,
									//    PARAM_ONE,
									//    PARAM_TWO,
									//    ...
									//    PARAM_N,
									//    };

enum {
	MQTTPUB_TIME_ID = 0,
};

struct ThreadData {
	string data;
	MQTTConf conf;
};


class CMQTTPub : public Theron::Actor
{
public:
	int m_id;
	Theron::Framework t_framwork;
	CMQTTPub() : Theron::Actor(t_framwork) {

	}
	explicit CMQTTPub(Theron::Framework &framwork) : Theron::Actor(framwork)
	{
		RegisterHandler(this, &CMQTTPub::handleMQTTResult);
	}

	void start();

	int setID(int id);
	int getID();

	void setTimer(int millisecond, int timeid);
	void cancelTimer(int timeid);
	void static onTimer(void *args);

	void handleMQTTResult(const MQTTResult& json, const Theron::Address from);
	void pub();
private:
	CMQTTClent client;

	void initMQTT();

private:
	mutex m_mtx;
	vector<ThreadData> m_threadData;	//用于保存pub线程接收的数据

private:
	map<int, Timer*> m_timer_map;
	map<int, string> m_timerid_map;  //to save timeid in string permanently

	map<int, int> int_timerid_map;   //to save timeid in int permanently,so it can be use in ArgumentM
	map<int, ArgumentM> m_timeargs_map; // a timeid relate a map of argumemts(ArgumentV),so it can be use in onTimer()
};

#endif